<?php

namespace flat\traits;

trait PluginController {
    
    /**
     * 
     */
    private $_pluginName = null;
    
    /**
     * 
     * @param string $image Image file path
     */
    public function getImage($image){
        $pn = $this->_getPluginName();
        return \PLUGINS_URL."{$pn}resources/img/$image";
    }
    
    /**
     * 
     * @param string $css Stylesheet path
     */
    public function loadCss($css){
        $pn = $this->_getPluginName();
        \flat\CssLoader::add(\PLUGINS_URL."{$pn}resources/css/$css");
    }
    
    /**
     * 
     * @param string $js Script path
     */
    public function loadJs($js){
        $pn = $this->_getPluginName();
        \flat\JsLoader::add(\PLUGINS_URL."{$pn}resources/js/$js");
    }
    
    /**
     * 
     * @param string $action
     * @return string
     */
    public function getViewFilename($action){
        
        $controllerName = $this->getControllerName();
        $pn = $this->_getPluginName();        
        return \PLUGINS_PATH."$pn/view/$controllerName/$action.phtml";
        
    }
    
    /**
     * 
     * @return string
     */
    private function _getPluginName(){
        
        if(\is_null($this->_pluginName)){
            
            $dir = \dirname((new \ReflectionClass($this))->getFileName());
            $this->_pluginName = \basename(\realpath($dir.'/..')).'/';
            
        }
        return $this->_pluginName;
        
    }
    
}
